package com.tma.android.gpm.data.database.handler;

import com.tma.android.gpm.data.database.util.DatabaseHandler;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class TableAdapter {
	/** The m db helper. */
	protected static DatabaseHandler	mDbHelper;
	/** The m db. */
	protected SQLiteDatabase		mDb;
	/** The m ctx. */
	protected final Context			mCtx;

	/**
	 * Open.
	 * 
	 * @return the database adapter
	 */
	public TableAdapter open() {

		if (mCtx == null)
			Log.v("vhgiang", "null value");
		mDbHelper = new DatabaseHandler(mCtx);
		try {
			if (mDb != null && mDb.isOpen())
				mDb.close();
			mDb = mDbHelper.getWritableDatabase();
		} catch (Exception e) {
			if (mDb != null && mDb.isOpen())
				mDb.close();
			mDb = mDbHelper.getReadableDatabase();
		} finally {
//			mDb.execSQL("PRAGMA synchronous=OFF");
//			mDb.setLockingEnabled(false);
//			mDb.beginTransaction();
		}
		// An Le Rework
		return this;
	}

	// close db
	/**
	 * Close.
	 */
	public void close() {

		if (mDb != null) {
//			if (mDb.inTransaction()) {
//				mDb.setTransactionSuccessful();
//				mDb.endTransaction();
//				mDb.setLockingEnabled(true);
//				mDb.execSQL("PRAGMA synchronous=NORMAL");
//			}
			mDb.close();
		}
	}

	/**
	 * Table exist.
	 * 
	 * @param table
	 *            the table
	 * @return true, if successful
	 */
	public boolean tableExist(String table) {

		Cursor cursor = mDb.rawQuery("SELECT * FROM " + table, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}

	/**
	 * Number row in database.
	 * 
	 * @param table
	 *            the table
	 * @return the int
	 */
	public int numberRowInDatabase(String table) {

		int count = 0;
		this.open();
		Cursor cursor = null;
		try {
			cursor = mDb.rawQuery("SELECT * FROM " + table, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.close();
				count = cursor.getCount();
			}
		} finally {
			if (cursor != null)
				cursor.close();
			this.close();
		}
		return count;
	}

	public int updatedPostStatus(String table) {

		String SQL_STATEMENT = "SELECT COUNT(*) FROM " + table;
		int count = 0;
		this.open();
		Cursor cursor = null;
		try {
			cursor = mDb.rawQuery(SQL_STATEMENT, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				count = cursor.getInt(0);
			}
		} finally {
			if (cursor != null)
				cursor.close();
			this.close();
		}
		return count;
	}

	public int updatedPostStatus(String table, String where, int value) {

		String SQL_STATEMENT = "SELECT COUNT(*) FROM " + table + " WHERE " + where + " = " + value;
		int count = 0;
		this.open();
		Cursor cursor = null;
		try {
			cursor = mDb.rawQuery(SQL_STATEMENT, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				count = cursor.getInt(0);
			}
		} finally {
			if (cursor != null)
				cursor.close();
			this.close();
		}
		return count;
	}

	public int updatedPostStatus(String table, String where1, int value1, String where2, int value2) {

		String SQL_STATEMENT = "SELECT COUNT(*) FROM " + table + " WHERE " + where1 + " = " + value1 + " and " + where2 + " = " + value2;
		int count = 0;
		this.open();
		Cursor cursor = null;
		try {
			cursor = mDb.rawQuery(SQL_STATEMENT, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				count = cursor.getInt(0);
			}
		} finally {
			if (cursor != null)
				cursor.close();
			this.close();
		}
		return count;
	}

	/**
	 * Instantiates a new database adapter.
	 * 
	 * @param context
	 *            the context
	 */
	public TableAdapter(Context context) {

		mCtx = context;
	}
}
